package com.lhj.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhj.crm.entity.Employee;
import com.lhj.crm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FYI
 * @since 2020-03-23
 */
@RestController
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */

    @GetMapping("/emp")
    public Map getList(Integer page,Integer limit ){
        Map<String,Object> map = new HashMap<String,Object> ();

        //设置mybatisPlus分页
        Page<Employee> employeePage = new Page<Employee>();
        //设置每页记录数
        employeePage.setSize(limit);
        //设置当前页码
        employeePage.setCurrent(page);

        IPage<Employee> iPage = employeeService.selectList(employeePage);

        map.put("code",0);
        map.put("msg","succeed");
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());

        return map;
    }
    /**
     *  新增用户
     */
    @PostMapping("/emp")
    public Map add(Employee employee,Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",employeeService.add(employee,roleId));
        return result;
    }

    /**
     *  删除用户
     */
    @DeleteMapping("/emp/del/{empId}")
    public Map del(@PathVariable Integer empId){
        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",employeeService.updateById(employee));
        return result;
    }

    /**
     * 修改用户
     * @param employee
     * @param roleId
     * @return
     */
    @PutMapping("/emp")
    public Map edit(Employee employee,Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",employeeService.update(employee,roleId));
        return result;
    }
}

